<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of main
 *
 * @author Maulnick
 */
class User extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper('url','form');
        $this->load->model('User_Model');
        $this->load->library('session');
    }
    public function index() {
        //$this->home();
    }
    /*** 
     * 
     * /User/xxx_page()
     * 
     * ***/
    public function register_page(){
        $title = 'register';
        // check session
        $tempsession = $this->session->userdata('user');
        if($tempsession){
            show_404();
            return;
        }else{
            // if not login
            $leftpage['page'] = 'login';
            $middlepage['page'] = 'register';
            $rightpage['page'] = 'blank';
            $this->show_page($title, $leftpage, $middlepage, $rightpage);
        }
    }
    public function register_succeed_page(){
        $title = 'register';
        $leftpage['page'] = 'login';
        $middlepage['page'] = 'regsucceed';
        $rightpage['page'] = 'blank';
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
        
    }
    public function register_failed_page(){
        $title = 'register';
        $leftpage['page'] = 'login';
        $middlepage['page'] = 'regfailed';
        $rightpage['page'] = 'blank';
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    public function login_page(){
        $data['title'] = $this->session->userdata('active_page');
    }
    public function login_failed_page(){
        $data['title'] = $this->session->userdata('active_page');
        $leftpage['page'] = 'login';
        $middlepage['page'] = 'loginfailed';
        $rightpage['page'] = 'blank';
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    /*** 
     * 
     * /User/function()
     * 
     * ***/
    public function register(){
        $user = array();
        $user['name']=$_POST['name'];
        $user['address']=$_POST['address'];
        $user['city']=$_POST['city'];
        $user['postalcode']=$_POST['postcode'];
        $user['telephone']=$_POST['telp'];
        $user['handphone']=$_POST['hp'];
        $user['fax']=$_POST['fax'];
        $user['email']=$_POST['email'];
        
        $admin = array();
        $admin['username']=$_POST['username'];
        $admin['password']= md5($_POST['passwords']);
        $admin['role_id'] = $_POST['role_id'];
        // insert data to database
        $step1 = $this->User_Model->insert_registration($user, $admin);
        // make folder
        $structure = 'assets/documents/process/';
        if($admin['role_id']==ROLE_CONSIGNEE){
            $sentstructure = $structure .'Consignee/' .$admin['username'] .'/Sent/';
            $inboxstructure = $structure .'Consignee/' .$admin['username'] .'/Inbox/';
            $docstructure = $structure .'Consignee/' .$admin['username'] .'/Doc/';
        }else if($admin['role_id']==ROLE_SHIPPINGLINE){
            $sentstructure = $structure .'ShippingLine/' .$admin['username'] .'/Sent/';
            $inboxstructure = $structure .'ShippingLine/' .$admin['username'] .'/Inbox/';
            $docstructure = $structure .'ShippingLine/' .$admin['username'] .'/Doc/';
        }else if($admin['role_id']==ROLE_OPERATORTERMINAL){
            $sentstructure = $structure .'OperatorTerminal/' .$admin['username'] .'/Sent/';
            $inboxstructure = $structure .'OperatorTerminal/' .$admin['username'] .'/Inbox/';
            $docstructure = $structure .'OperatorTerminal/' .$admin['username'] .'/Doc/';
        }else if($admin['role_id']==ROLE_TRUCKING){
            $sentstructure = $structure .'Trucking/' .$admin['username'] .'/Sent/';
            $inboxstructure = $structure .'Trucking/' .$admin['username'] .'/Inbox/';
            $docstructure = $structure .'Trucking/' .$admin['username'] .'/Doc/';
        }
        $step2 = mkdir($sentstructure, 0, true);
        $step3 = mkdir($inboxstructure, 0, true);
        $step4 = mkdir($docstructure,0,true);
        if ($step1 && $step2 && $step3 && $step4){
            redirect( 'user/register_succeed_page' );
        }else{
            redirect( 'user/register_failed_page' );
        }
         
    }
    public function login(){
        $this->form_validation->set_rules('username','username','required|trim|max_length[50]|xss_clean');
        $this->form_validation->set_rules('password','password','required|trim|max_length[200]|xss_clean');
        if($this->form_validation->run()==false){
            // if username and password not filled
            redirect( 'user/login_page' );
        }else{
            $user = $this->User_Model->get_validation();
            if($user==false){
                // if login not valid
                redirect( 'user/login_failed_page' );
            }else{
                // if login succeed
                // setting session
                $data = array(
                    'username' => $user['username'],
                    'user_id' => $user['user_id'],
                    'role_id' => $user['role_id'],
                    'is_logged_in' => true
                );
                $this->session->set_userdata('user',$data);
                redirect( 'main/home_page' );
            }
        }
    }
    public function logout() {
        $this->session->unset_userdata('user');
        redirect();
    }
    public function validateusername(){
        $username = $_POST['username'];
        if($this->User_Model->check_username($username)){
            echo 'exist';
        }else{
            echo 'notexist';
        }
    }
    private function show_page($title, $leftpage, $middlepage, $rightpage){
        $data['title'] = $title;
        $data['topbar'] = $this->load->view('topbar/home','',TRUE);
        $data['rightpage'] = $this->load->view('mainpage/bodyright',$rightpage,TRUE);
        $data['middlepage'] = $this->load->view('mainpage/bodymiddle',$middlepage,TRUE);
        $data['leftpage'] = $this->load->view('mainpage/bodyleft',$leftpage,TRUE);
        $this->load->view('front',$data);
    }
}
